package cq.zhcqk.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jimi_wu.ptlrecyclerview.Divider.BaseItemDecoration;
import com.jimi_wu.ptlrecyclerview.HeaderAndFooter.OnItemClickListener;
import com.jimi_wu.ptlrecyclerview.HeaderAndFooter.OnItemLongClickListener;
import com.jimi_wu.ptlrecyclerview.PullToLoad.OnLoadListener;
import com.jimi_wu.ptlrecyclerview.PullToLoad.PullToLoadRecyclerView;
import com.jimi_wu.ptlrecyclerview.PullToRefresh.OnRefreshListener;
import com.jimi_wu.ptlrecyclerview.SimpleAdapter.SimpleAdapter;
import com.jimi_wu.ptlrecyclerview.SimpleAdapter.ViewHolder;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.ImageLoaderHelper;
import cq.zhcqk.util.StringUtil;

/**
 * Created by zhangle on 2018/3/22.
 */
public class GzyyListActivity extends BaseActivity implements View.OnClickListener{
    private PullToLoadRecyclerView rcv;
    private ArrayList<Map<String, Object>> list;
    private int page = 1;
    private Map<String, Object> data = null;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_zw_list);
        data = (Map<String, Object>) getIntent().getSerializableExtra("list");
        initBack();
        initRecyclerView();
    }

    @Override
    protected void initData() {
        ArrayList<Map<String, Object>> listNew = null;
        listNew = (ArrayList<Map<String, Object>>)(data.get("data"));
        if(listNew == null || listNew.size()==0){
            Toast.makeText(context,"没有更多",Toast.LENGTH_SHORT).show();
        }else{
            list.addAll(listNew);
        }
        rcv.completeRefresh();
        rcv.completeLoad(1);
    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("工作办理");
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
        }
    }

    private void initRecyclerView(){
        rcv = (PullToLoadRecyclerView)findViewById(R.id.rcv);

        rcv.setLayoutManager(new GridLayoutManager(context,1));
        rcv.setRefreshEnable(false);
        rcv.setLoadEnable(false);
//        设置适配器，封装后的适配器只需要实现一个函数
        list = new ArrayList<>();
        rcv.setAdapter(new SimpleAdapter<Map<String, Object>>(context, list, R.layout.item_gzyy_list) {
            @Override
            protected void onBindViewHolder(ViewHolder holder, Map<String, Object> data) {
                String imgUrl = data.get("imgUrl")+"";
                if(!StringUtil.isNull(imgUrl)){
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.VISIBLE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.GONE);
                    ImageLoader.getInstance().displayImage(StringUtil.checkImgeUrl(data.get("imgUrl")+""), holder.<ImageView>getView(R.id.item_header_img), ImageLoaderHelper.getOptionsDf());
                }else{
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.GONE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.GONE);
                }
                holder.<TextView>getView(R.id.item_title).setText(""+data.get("name"));
            }
        });
//        设置刷新监听
        rcv.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onStartRefreshing() {
            }
        });
//        设置加载监听
        rcv.setOnLoadListener(new OnLoadListener() {
            @Override
            public void onStartLoading(int skip) {

            }
        });
//        设置分割线
        rcv.addItemDecoration(new BaseItemDecoration(context,R.color.gray_bg));

        rcv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                Intent intent = new Intent(context, GzyyListDetailActivity.class);
                intent.putExtra("id",(int) Double.parseDouble(String.valueOf(list.get(position).get("dataid")))+"");
                intent.putExtra("departId",(int) Double.parseDouble(String.valueOf(list.get(position).get("departId")))+"");
                startActivity(intent);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
            }
        });
        rcv.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(int position) {
                return true;
            }
        });
    }

    private void getCode(int offset,String type){
        RequestParams params = new RequestParams();
        params.add("limit", Common.PAGE_SIZE);
        params.add("offset",offset+"");
        params.add("type",type);
        httpClient.post(Common.URL+Common.NEWS_LIST,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                ArrayList<Map<String, Object>> listNew = null;
                listNew = (ArrayList<Map<String, Object>>)((Map<String, Object>)map.get("data")).get("rows");
                if(listNew == null || listNew.size()==0){
                    Toast.makeText(context,"没有更多",Toast.LENGTH_SHORT).show();
                }else{
                    list.addAll(listNew);
                    page ++ ;
                }
                rcv.completeRefresh();
                rcv.completeLoad(1);

            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure："+errorResponse);
            }
        });
    }
}
